﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Reflection;
using System.Windows.Controls;
using System.Windows;

namespace AddressKeyBoard
{
    public class XButton
    {
        #region static Functions
        public static Color GetColor(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return Colors.White;
            }

            value = value.Trim();

            if (value.StartsWith("#"))
            {
                System.Drawing.Color newColor = System.Drawing.ColorTranslator.FromHtml(value);
                return Color.FromArgb(newColor.A, newColor.R, newColor.G, newColor.B);
            }            
            else if (value.Contains("(") && value.Contains(")"))
            {
                int begin = value.IndexOf("(");
                int end = value.IndexOf(")");
                string realVal = value.Substring(begin + 1, end - begin - 1);
                string[] arr = realVal.Split(',');
                if (arr.Length == 3)
                {
                    byte r = Convert.ToByte(arr[0]);
                    byte g = Convert.ToByte(arr[1]);
                    byte b = Convert.ToByte(arr[2]);
                    return Color.FromRgb(r, g, b);
                }
                if (arr.Length == 4)
                {
                    byte a = Convert.ToByte(arr[0]);
                    byte r = Convert.ToByte(arr[1]);
                    byte g = Convert.ToByte(arr[2]);
                    byte b = Convert.ToByte(arr[3]);
                    return Color.FromArgb(a, r, g, b);
                }
            }

            if (!value.Contains(","))
            {
                Type colorType = typeof(Color);
                PropertyInfo info = colorType.GetProperty(value, BindingFlags.Public | BindingFlags.Static);
                if (info != null)
                {
                    return (Color)info.GetValue(null, null);
                }
                Type colorType1 = typeof(System.Windows.Media.Colors);
                PropertyInfo info1 = colorType1.GetProperty(value, BindingFlags.Public | BindingFlags.Static);
                if (info1 != null)
                {
                    return (Color)info1.GetValue(null, null);
                }
            }
            else
            {
                string[] array = value.Split(',');
                if (array.Length == 3)
                {
                    byte r = Convert.ToByte(array[0]);
                    byte g = Convert.ToByte(array[1]);
                    byte b = Convert.ToByte(array[2]);
                    return Color.FromRgb(r, g, b);
                }
                if (array.Length == 4)
                {
                    byte a = Convert.ToByte(array[0]);
                    byte r = Convert.ToByte(array[1]);
                    byte g = Convert.ToByte(array[2]);
                    byte b = Convert.ToByte(array[3]);
                    return Color.FromArgb(a, r, g, b);
                }
            }
            return Colors.White;
        }

        public static int GetInt32Value(string value, int defaultVal)
        {
            if (string.IsNullOrEmpty(value))
            {
                return defaultVal;
            }
            int result = defaultVal;
            int.TryParse(value, out result);
            return result;
        }

        public static double GetDoubleValue(string value, double defaultVal)
        {
            if (string.IsNullOrEmpty(value))
            {
                return defaultVal;
            }
            double result = defaultVal;
            double.TryParse(value, out result);
            return result;
        }
        #endregion

        public string Name { get; set; }
        public string Left { get; set; }
        public string Top { get; set; }
        public string Text { get; set; }
        public string FontSize { get; set; }
        public string Width { get; set; }
        public string Height { get; set; }
        public string BackColor { get; set; }
        public string ForeColor { get; set; }
        public string Tag { get; set; }
     
        public XButton() { }

        public Button Creat(Canvas parent)
        {
            double fontSize = GetFontSize();
            TextBlock tb = new TextBlock()
            {
                Text = this.Text,
                FontSize = fontSize,
                TextWrapping = TextWrapping.Wrap,
                TextAlignment = TextAlignment.Center
            };

            Button btn = new Button
            {
                Name = "button" + this.Name,
                Content = tb,
                Height = GetHeight(),
                Width = GetWidth(),
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(10, 10, 0, 0),
                VerticalAlignment = VerticalAlignment.Top,
                Visibility = Visibility.Visible,
                FontSize = fontSize,
                Background = Brushes.LightSkyBlue,
                Tag = this.Tag
            };
            parent.Children.Add(btn);
            btn.SetValue(Canvas.LeftProperty, (double)GetLeft());
            btn.SetValue(Canvas.TopProperty, (double)GetTop());
            return btn;      
        }

        public int GetTop()
        {
            return GetInt32Value(Top, 36);
        }

        public int GetLeft()
        {
            return GetInt32Value(Left, 36);
        }

        public double GetFontSize()
        {
            return GetDoubleValue(FontSize, 36D);
        }

        public int GetWidth()
        {
            return GetInt32Value(Width, 120);
        }

        public int GetHeight()
        {
            return GetInt32Value(Height, 100);
        }

        public Brush GetBackColor()
        {
            Color color =  GetColor(BackColor);
            SolidColorBrush myBrush = new SolidColorBrush(color);
            return myBrush;
        }

        public Brush GetForeColor()
        {
            Color color = GetColor(ForeColor);
            SolidColorBrush myBrush = new SolidColorBrush(color);
            return myBrush;
        }
    }
}
